package com.example.pingmiantutui.ui.mistake_collection

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.example.pingmiantutui.R
import com.example.pingmiantutui.data.MistakeQuestion

class MistakeQuestionAdapter : BaseMistakeQuestionAdapter() {
    private var onItemClick: ((MistakeQuestion) -> Unit)? = null

    fun setOnItemClickListener(listener: (MistakeQuestion) -> Unit) {
        onItemClick = listener
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return when (viewType) {
            VIEW_TYPE_SINGLE -> SingleQuestionViewHolder(
                LayoutInflater.from(parent.context)
                    .inflate(R.layout.item_single_question, parent, false)
            )
            VIEW_TYPE_CONTRAST -> ContrastQuestionViewHolder(
                LayoutInflater.from(parent.context)
                    .inflate(R.layout.item_contrast_question, parent, false)
            )
            VIEW_TYPE_ANALYSE -> AnalyseQuestionViewHolder(
                LayoutInflater.from(parent.context)
                    .inflate(R.layout.item_analyse_question, parent, false)
            )
            else -> throw IllegalArgumentException("Unknown view type: $viewType")
        }
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        val question = getItem(position)
        when (holder) {
            is SingleQuestionViewHolder -> {
                holder.bind(question)
                holder.itemView.setOnClickListener { onItemClick?.invoke(question) }
            }
            is ContrastQuestionViewHolder -> {
                holder.bind(question)
                holder.itemView.setOnClickListener { onItemClick?.invoke(question) }
            }
            is AnalyseQuestionViewHolder -> {
                holder.bind(question)
                holder.itemView.setOnClickListener { onItemClick?.invoke(question) }
            }
        }
    }
}